package com.intel.two.service.camera.outdoor.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.bo.camera.CameraBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.mapper.camera.outdoor.FctMapperO;
import com.intel.two.pojo.camera.floor.FloorCameraPojo;
import com.intel.two.service.camera.outdoor.FctServiceO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service("fctServiceImplO")
public class FctServiceImplO implements FctServiceO {
    @Autowired
    @Qualifier("fctMapperO")
    private FctMapperO fctMapper;
    @Override
    public LayuiPageVo<FloorCameraPojo> findEveryFloor(Map<Object, Object> map) {
        Integer page = Integer.parseInt(map.get("page")+"");
        Integer limit = Integer.parseInt(map.get("limit")+"");

        PageHelper.startPage(page,limit);

        List<FloorCameraPojo> list = fctMapper.findFloorCamera(map);
        System.out.println("List查到的数据：" + list);
        PageInfo<FloorCameraPojo> objectPageInfo = new PageInfo<>(list);
        LayuiPageVo<FloorCameraPojo> layuiPageVo = new LayuiPageVo<>();
        layuiPageVo.setMsg("楼层监控信息");
        layuiPageVo.setCode(0);
        layuiPageVo.setCount(objectPageInfo.getTotal());
        layuiPageVo.setData(objectPageInfo.getList());
        System.out.println("返回给前台的数据：" + layuiPageVo);
        return layuiPageVo;
    }

    @Override
    public R delSpecifiedFloor(String id) {
        if (fctMapper.delSpecifiedFloor(Integer.parseInt(id)) != 0 && fctMapper.delSpecifiedFloor(Integer.parseInt(id)) <= 1) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    public R addFloor (FloorCameraPojo floorCameraPojo) {
        int i = fctMapper.addFloor(floorCameraPojo);
        if (i != 0 && i <= 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Override
    public R updFloor(FloorCameraPojo floorCameraPojo) {
        int i = fctMapper.updFloor(floorCameraPojo);
        if (i != 0 & i <= 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Override
    public R deletePetAll(Map<String, Object> arrData) {
        String str = (String) arrData.get("id");
        String[] stirs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < stirs.length; i++) {
            list.add(Integer.parseInt(stirs[i]));
        }
        System.out.println("删除多个的list集合：" + list);
        int i = fctMapper.delAllFloor(list);
        if (i == list.size()) {
            return R.ok();
        } else {
            return R.error();
        }


    }

    @Override
    public R brandBarData() {
        R brandBar = R.ok().data("brandBar", fctMapper.brandBarData());
        System.out.println("返回的数据brandBar：" + brandBar);
        return brandBar;
    }

    @Override
    public R brandPieData() {
        R brandPie = R.ok().data("brandPie", fctMapper.brandPieData());
        System.out.println("返回的数据brandPie：" + brandPie);
        return brandPie;
    }

    @Override
    public List<CameraBo> studentExcelData(FloorCameraPojo floor) {
        List<FloorCameraPojo> floorList = fctMapper.studentExcelData(floor);
        //把po 转为 bo
        List<CameraBo> collect = floorList.stream()
                .map((col) -> {
                    CameraBo cameraBo = new CameraBo();
                    BeanUtils.copyProperties(col,cameraBo);
                    return cameraBo;
                }).collect(Collectors.toList());
        return collect;
    }

}
